package com.hxexp.weixin.mvc.faq.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.common.web.HttpUtil;
import com.hxexp.weixin.constant.StartStationsId;
import com.hxexp.weixin.constant.StringConstant;
import com.hxexp.weixin.constant.WebAppVersion;
import com.hxexp.weixin.mvc.common.service.CommonSerivce;
import com.hxexp.weixin.mvc.faq.model.FAQCategory;
import com.hxexp.weixin.mvc.faq.model.FAQItem;
import com.hxexp.weixin.mvc.faq.service.FAQService;

/**
 * FAQ信息
 *
 * @author chengang
 * @author li.jiang
 */
@Controller
@RequestMapping(value = "/faq")
public class FAQController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FAQController.class);
    @Autowired
    private FAQService faqService;
    @Autowired
    private CommonSerivce commonSerivce;

    /**
     * 帮助分类列表
     *
     * @return 帮助分类列表页面
     */
    @RequestMapping(value = "/index")
    public String index(@RequestParam(value = StringConstant.APP_VERSION_PARAM_NAME, defaultValue = WebAppVersion.HX) String ver,
                        HttpServletRequest request,
                        Model model) {
        if (!StringUtils.isEmpty(ver)) {
            String faqFileURI = WebAppVersion.FAQ_FILE_MAP.get(ver);
            if (faqFileURI != null) {
                String faqFileRealPath = HttpUtil.getFileRealPath(request, faqFileURI);
                try {
                    List<FAQCategory> faqCategoryList = faqService.getFAQList(faqFileRealPath);
                    model.addAttribute("faqCategoryList", faqCategoryList);
                    model.addAttribute("ver", ver);
                } catch (ServiceException e) {
                    LOGGER.debug("获取帮助信息列表发生异常", e);
                }
            }
        }
        return "faq_list";
    }

    /**
     * 帮助详情
     *
     * @return 帮助详情页面
     */
    @RequestMapping(value = "/detail")
    public String detail(@RequestParam(value = StringConstant.APP_VERSION_PARAM_NAME, defaultValue = WebAppVersion.HX) String ver, String categoryId,
                         String categoryName, String origin, String startStationId, HttpServletRequest request, Model model) {
        if (!StringUtils.isEmpty(ver)) {
            String faqFileURI = WebAppVersion.FAQ_FILE_MAP.get(getFaqFileVersion(ver, startStationId, categoryId));
            if (faqFileURI != null) {
                String faqFileRealPath = HttpUtil.getFileRealPath(request, faqFileURI);
                try {
                    List<FAQItem> faqItemList = faqService.getFAQItems(faqFileRealPath, categoryId);
                    model.addAttribute("faqItemList", faqItemList);
                    model.addAttribute("ver", ver);
                    model.addAttribute("categoryName", categoryName);
                } catch (ServiceException e) {
                    LOGGER.debug("获取帮助条目发生异常", e);
                }
            }
        }
        model.addAttribute("origin", origin);
        return "faq_detail";
    }

    private String getFaqFileVersion(String ver, String startStationId, String categoryId) {
        if (WebAppVersion.HX.equals(ver) && ("005".equals(categoryId))) {
            if (commonSerivce.isNCStartStation("", startStationId)) {
                return WebAppVersion.NC;
            } else if (commonSerivce.isDYStartStation(startStationId)) {
                return WebAppVersion.DY;
            } else if (StartStationsId.AY.equals(startStationId)) {
                return WebAppVersion.AY;
            } else if (commonSerivce.isXAStartStation("", startStationId)) {
                return WebAppVersion.XA;
            } else {
                return ver;
            }
        } else {
            return ver;
        }
    }
}
